<template>
  <span @click="logsFlag?'':handleOpen()">
    <el-badge :value="logsFlag?'':logsLen"
              :max="99">
      <i class="icon-rizhi1"></i>
    </el-badge>
    <el-dialog title="日志"
               v-model="box"
               width="60%"
               :close-on-click-modal="false"
               append-to-body>
      <el-button type="primary"
                 icon="el-icon-upload"
                 @click="send">上传服务器</el-button>
      <el-button type="danger"
                 icon="el-icon-delete"
                 @click="clear">清空本地日志</el-button>
      <el-table :data="logDataSource">
          <el-table-column type="index"
                           label="序号"
                           :index="loadIndex"
                           width="50px">
        </el-table-column>
        <el-table-column prop="type"
                         label="类型"
                         width="50px">
        </el-table-column>
        <el-table-column prop="url"
                         label="地址"
                         show-overflow-tooltip
                         width="180">
        </el-table-column>
        <el-table-column prop="message"
                         show-overflow-tooltip
                         label="内容">
        </el-table-column>
        <el-table-column prop="stack"
                         show-overflow-tooltip
                         label="错误堆栈">
        </el-table-column>
        <el-table-column prop="time"
                         label="时间">
        </el-table-column>
      </el-table>
         <el-pagination
             :hide-on-single-page="logsLen<1"
             :total="logsLen"
             v-model:current-page="page.current"
             v-model:page-size="page.size"
             layout="prev, pager, next"
             @size-change="handleSizeChange"
             @current-change="handleCurrentChange"
         />
    </el-dialog>
  </span>
</template>

<script>
import {mapState} from "pinia";
import useLogStore from "@/stores/useLogStore";

export default {
  name: "top-logs",
  data() {
    return {
      box: false,
      logDataSource: [],
      page: {
        current: 1,
        size: 10
      },
    };
  },
  created() {
  },
  mounted() {
  },
  computed: {
    ...mapState(useLogStore, ['logsList', "logsFlag", "logsLen"])
  },
  props: [],
  methods: {
    loadIndex(val) {
      let currentStart = (this.page.current - 1) * this.page.size
      val = currentStart + val + 1
      return val
    },
    handleOpen() {
      this.box = true;
      this.loadData()
    },
    handleSizeChange(val) {
      this.loadData()
    },
    handleCurrentChange(val) {
      this.loadData()
    },
    loadData() {
      console.log("page", this.page)
      let startIndex = (this.page.current - 1) * this.page.size
      let endIndex = startIndex + this.page.size;
      let dataSource = [];
      if (endIndex > this.logsLen) {
        dataSource = this.logsList.slice(startIndex)
      } else {
        dataSource = this.logsList.slice(startIndex, endIndex)
      }
      this.logDataSource = dataSource

    },
    send() {
      this.$confirm("确定上传本地日志到服务器?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return useLogStore().SendLogs();
      }).then(() => {
        this.box = false;
        this.$message.success("发送成功");
      }).catch(() => {
      });
    },
    clear() {
      this.$confirm("确定清空本地日志记录?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        useLogStore().clearLogs()
        this.box = false;
        this.$message.success("清空成功");
      }).catch(() => {
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.code {
  font-size: 12px;
  display: block;
  font-family: monospace;
  white-space: pre;
  margin: 1em 0px;
}
</style>
